<template>

    <yplayout v-if="isLoad">
        <template #header="head">
        <div class="flex items-center">
            <i class="ri-2x text-gray-500 ml-2 ri-list-unordered"></i>
            <div class="text-xl px-3">无限分类</div>
            <div class="text-gray-400">编辑</div>
        </div>
        <div class="flex-1"></div>
        </template>
        <template #list>

            <div class="text-sm">
                <div class="mt-8 space-y-2">
                    <div class="flex items-center">
                        <div class="w-24 text-right pr-5">所属分类</div>
                        <div class="flex-1">
                            <Parent controller="classify" v-model="formData.pid" :pid="formData.pid" colkey="title" defaultTitle="--选择分类--"></Parent>
                        </div>
                    </div>
                    <div class="flex">
                        <div class="w-24 text-right pr-5">名称</div>
                        <div class="flex-1" id="title">
                            <ypinput v-model="formData.title"></ypinput>
                        </div>
                    </div>
                    <div class="flex">
                        <div class="w-24 text-right pr-5">绑定栏目</div>
                        <div class="flex-1" id="title">
                            <ypinput v-model="formData.category_id"></ypinput>
                        </div>
                    </div>

                </div>
                <div class="flex justify-center py-3 space-x-2">
                    <button class="btn btn-chen" type="button" @click="save">
                        <i class="ri-save-line ri-lg pr-1"></i>
                        保存
                    </button>
                    <router-link to="list" class="btn">取消</router-link>
                </div>
            </div>
        </template>
    </yplayout>
</template>
<script setup>
import { postData,alter } from '@/common/base'
import { router } from "@/router"
import { getData } from '@/common/base'
import { ref, reactive, onMounted } from 'vue'
import Parent from './parent.vue'


const isLoad = ref(false)
const id = router.currentRoute.value.query.id
const formData = reactive({})

const getres = async(id) => {
    const res= await getData('classify/read',{id:id})
    Object.keys(res.data).map((k)=>{
        formData[k]=res.data[k]
    })

}

const getTree=async()=>{

    const resp = await getData('/classify/gettree', { id: formData.id })
    menu.value = resp.data


}
  //增加分类
  const save=async ()=>{
    const resp= await postData("classify/update",formData)
    if(resp.code==1){
      alter({ type: 'alter-success', text: resp.msg })
      router.push('/classify/list')
    }
  }

const menu = ref([])
onMounted(async () => {
    await getres(id)
    await getTree()

    isLoad.value = true
})
</script>
